home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 882 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.8 KB  |  58 lines

  1. Newsgroups: comp.lang.c
  2. Path: in1.uu.net!bcstec!schultz
  3. From: schultz@bcstec.ca.boeing.com (Robert A. Schultz)
  4. Subject: Re: How to remove a substring from a string
  5. Message-ID: <DKxL38.8K1@bcstec.ca.boeing.com>
  6. Organization: The Boeing Company
  7. X-Newsreader: TIN [version 1.2 PL2]
  8. References: <4cu5br$ejb@erinews.ericsson.se>
  9. Date: Tue, 9 Jan 1996 20:33:07 GMT
  10.  
  11. Inger Dufva Z/OF (eraida@lmera.ericsson.se) wrote:
  12. : Hi,
  13.  
  14. : I would like to remove a sub string from a string in C. An example:
  15.  
  16. : string: howdoyoudo
  17. : sub_string: you
  18.  
  19. : After this function (called remove_substring here) has been called:
  20.  
  21. : new_string = remove_substring(string, sub_string);
  22.  
  23. : new_string should have the value: howdodo
  24.  
  25. : Can anyone please help me with this function? Thanks in advance!
  26. : Cheers,
  27.  
  28. For shame!  There must be more to your homework problem text than this.  I.e.
  29. Destructive or non-destructive?  Just the first occurance of sub_string or
  30. all of them (Begs for a recursive solution.)?  Are there any "forbidden" calls
  31. to the ANSI standard functions?  If you want an "A" you are just going to have
  32. to be more specific.
  33.  
  34. Never-the-less both
  35.  
  36.    char *remove_substring(char *string, char *sub_string)
  37.    {
  38.       return "howdodo";
  39.    }
  40.  
  41. and
  42.  
  43.    char *remove_substring(char *string, char *sub_string)
  44.    {
  45.       char *cp;
  46.       if ((cp=strstr(string, sub_string)) != NULL)
  47.          memmove(cp, cp+strlen(sub_string), strlen(cp)-strlen(sub_string)+1);
  48.       return string;
  49.    }
  50.  
  51. satisfy the letter of your question if not the spirit. :)
  52.  
  53. -- 
  54. ='''                                            Rob Schultz
  55. c-oo                                            TSAP Programmer Guy
  56.    \                                            Boeing Tech Services Systems
  57.   -    veni vidi recodei                        (206) 544-0793 MS 2H-31
  58.